/***************************************************************************
 *  Copyright (c) 2005 Chih-Chung Chang                                    *
 *  Parts copyright (c) 2006 Michael Olson                                 *
 *  Copyright (C) 2007-2008 by Fehmi "Canavar" SAGLAM                      *
 *  canavar@fehmicans.net                                                  *
 *                                                                         *
 *  This file is part of sTerm.                                            *
 *                                                                         *
 *  sTerm is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation; either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  sTerm is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include "../include/stermkeyboard.h"

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermKeyboard::StermKeyboard( int section_count )
{
	this->section_count = section_count;
	this->cur_section_id = 0;
	for(int i=0; i<10; ++i)
		this->sections[i] = NULL;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermKeyboard::StermKeyboard( int section_count, StermSection *section_list[] )              
{
	this->section_count = section_count;
	this->cur_section_id = 0;
	for(int i=0; i<10; ++i)
		this->sections[i] = NULL;
	
   for(int i=0; i<section_count; ++i)
		this->AddSection(i, section_list[i]);  
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermKeyboard::~StermKeyboard()              
{
	for(int i=0; i<10; ++i)
		if(this->sections[i] != NULL)
		{
		    delete this->sections[i];
		    this->sections[i] = NULL;
      }  
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermKeyboard::AddSection( int section_id, StermSection *section )
{
	this->sections[section_id] = section;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermSection * StermKeyboard::GetCurrentSection()
{
	return this->sections[this->cur_section_id];
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
StermSection * StermKeyboard::GetSection(int section_id)
{
	return this->sections[section_id];
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermKeyboard::SelectSection( int section_id )
{
	if(this->sections[section_id] != NULL)
	{
		this->sections[section_id]->SelectButton();
		this->cur_section_id = section_id;
	}
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
void StermKeyboard::UnSelectSection( int section_id )
{
	if(this->sections[section_id] != NULL)
		this->sections[section_id]->UnSelect();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
int StermKeyboard::MoveLeft()
{    
	this->UnSelectSection(this->cur_section_id);

	if(this->cur_section_id == 0)
		this->cur_section_id = this->section_count - 1;
	else
		this->cur_section_id--;

	if(!this->GetSection(this->cur_section_id)->isSelectable()) 
	{
		this->MoveLeft();
		return 1;
	}

	this->SelectSection(this->cur_section_id);
	return 0;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
int StermKeyboard::MoveRight()
{
	this->UnSelectSection(this->cur_section_id);

	this->cur_section_id = (++this->cur_section_id)%this->section_count;

	if(!this->GetSection(this->cur_section_id)->isSelectable()) 
	{
		this->MoveRight();
		return 1;
	}

	this->SelectSection(this->cur_section_id);
	return 0;
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermKeyboard::Get_Val()
{
	return this->sections[cur_section_id]->Get_Val();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermKeyboard::Get_Shift_Val()
{
	return this->sections[cur_section_id]->Get_Shift_Val();
}

//----------------------------------------------------------------------------//
//                                                                            //
//----------------------------------------------------------------------------//
string StermKeyboard::Get_Ctrl_Val()
{
	return this->sections[cur_section_id]->Get_Ctrl_Val();
}

// Touchscreen select
void StermKeyboard::Select(int col, int row) {
    int sects[5] = { 4, 10, 20, 31, 53 };
    int i, scol, srow;
#ifndef __arm__
    printf("absolute col: %d, row: %d\n",col,row);
#endif
    if(row>20 && row<24 && col>0 && col<54) {
	for(i=0; i<5; i++) {
	    if(sects[i] > col) {
		if(i == 4) {
		    if(col<37) scol = (col - sects[i-1] - 1)/3;
		    else scol = 2 + (col - sects[i-1] - 7)/4;
		} else scol = col - sects[i-1] - 1;
		srow = row - 21;
#ifndef __arm__
		printf("section: %d, col: %d, row: %d \n", i, scol, srow);
#endif
		UnSelectSection(cur_section_id);
		cur_section_id = i;
		sections[i]->SelectButton(srow,scol);
		break;
	    }
	}
    }
}
